package top.shiyiri.blob;

import org.junit.Test;
import top.shiyiri.bean.Customer;
import top.shiyiri.util.JDBCUtils;

import java.io.*;
import java.sql.*;

/**
 * @author Aunean
 * @date 2022/1/19 17:56
 * @Description 测试使用 PreparedStatement 操作 Blob 类型的数据
 */

public class BlobTest {

    //向数据表 customers 中插入 Blob 类型的字段
    @Test
    public void test1() {
        PreparedStatement ps = null;
        FileInputStream fis = null;
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";
            ps = conn.prepareStatement(sql);

            // 填充占位符
            ps.setString(1, "隶属");
            ps.setString(2, "xhq@126.com");
            ps.setDate(3, new Date(new java.util.Date().getTime()));
            // 操作Blob类型的变量
            fis = new FileInputStream("C:\\Users\\14533\\Pictures\\Saved Pictures\\u1.png");
            ps.setBlob(4, fis);
            //执行
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert fis != null;
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            JDBCUtils.closeResource(conn, ps);
        }

    }

    //查询数据表 customers 中 Blob 类型的字段
    @Test
    public void test2() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        InputStream is = null;
        OutputStream os = null;

        try {
            conn = JDBCUtils.getConnection();
            String sql = "SELECT id, name, email, birth, photo FROM customers WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 21);
            rs = ps.executeQuery();
            if(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String email = rs.getString(3);
                Date birth = rs.getDate(4);
                Customer cust = new Customer(id, name, email, birth);
                System.out.println(cust);
                //读取Blob类型的字段
                Blob photo = rs.getBlob(5);
                is = photo.getBinaryStream();
                os = new FileOutputStream("c.jpg");
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os !=  null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            JDBCUtils.closeResource(conn, ps, rs);
        }
    }
}
